package com.wn.cloud.provider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wn.cloud.common.constant.ResponseEnum;
import com.wn.cloud.common.exception.GlobalException;
import com.wn.cloud.common.response.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @FileName: PayMentController
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/3/7 - 23:38
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@RestController
@RequestMapping("/payment/breaker")
// @DefaultProperties(defaultFallback = "defaultFallbackMethod")
public class PaymentController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "defaultFallbackMethod",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
            })
    public DataResult<Integer> getOne(@PathVariable("id") int id) {
        if (id < 0) {
            throw new GlobalException(ResponseEnum.ID_NOT_NEGATIVE);
        }
        return DataResult.genDataResult(ResponseEnum.SUCCESS, id);
    }

    public DataResult<Integer> defaultFallbackMethod(int id) {
        return DataResult.genDataResult(ResponseEnum.ID_NOT_NEGATIVE, id);
    }
}
